package com.industrial.premu.service.impl.sqsj;


import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.DictUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuAddSubstractItem;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.domain.PreMuBenefitEvaluationNo;
import com.industrial.premu.domain.PreMuCompanyInfo;
import com.industrial.premu.domain.sqsj.*;
import com.industrial.premu.mapper.PreMuAddSubstractItemMapper;
import com.industrial.premu.mapper.PreMuBenefitEvaluationMapper;
import com.industrial.premu.mapper.PreMuBenefitEvaluationNoMapper;
import com.industrial.premu.mapper.PreMuIndexWeightMainMapper;
import com.industrial.premu.mapper.sqsj.*;
import com.industrial.premu.service.*;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 涉企数据导入记录Service业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-24
 */
@Service
public class PreMuImportLogServiceImpl implements IPreMuImportLogService
{

    @Autowired
    private PreMuAddSubstractItemMapper preMuAddSubstractItemMapper;

    @Autowired
    private PreMuImportLogMapper preMuImportLogMapper;

    private static final int BATCH_SIZE = 100;

    @Autowired
    private PreMuImportBenefitEvaluationNoMapper preMuImportBenefitEvaluationNoMapper;

    @Autowired
    private IPreMuImportTongjiService preMuImportTongjiService;
    @Autowired
    private IPreMuImportRensheService preMuImportRensheService;

    @Autowired
    private IPreMuImportBenefitEvaluationService preMuImportBenefitEvaluationService;

    @Autowired
    private IPreMuCompanyInfoService preMuCompanyInfoService;

    @Autowired
    private IPreMuImportShuiwuService preMuImportShuiwuService;

    @Autowired
    private IPreMuImportShuiliService preMuImportShuiliService;

    @Autowired
    private IPreMuImportGuotuService preMuImportGuotuService;

    @Autowired
    private IPreMuImportHuanbaoService preMuImportHuanbaoService;

    @Autowired
    private PreMuIndexWeightMainMapper preMuIndexWeightMainMapper;

    @Autowired
    private PreMuImportAddMapper preMuImportAddMapper;

    @Autowired
    private PreMuImportAddSubstractMapper preMuImportAddSubstractMapper;

    @Autowired
    private PreMuImportSubstractMapper preMuImportSubstractMapper;

    @Autowired
    private PreMuImportVetoMapper preMuImportVetoMapper;

    @Autowired
    private PreMuBenefitEvaluationMapper preMuBenefitEvaluationMapper;

    @Autowired
    private PreMuBenefitEvaluationNoMapper preMuBenefitEvaluationNoMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private PreMuImportCustomIndexLjMapper preMuImportCustomIndexLjMapper;
    /**
     * 查询涉企数据导入记录
     *
     * @param id 涉企数据导入记录主键
     * @return 涉企数据导入记录
     */
    @Override
    public PreMuImportLog selectPreMuImportLogById(String id)
    {
        return preMuImportLogMapper.selectPreMuImportLogById(id);
    }

    /**
     * 查询涉企数据导入记录列表
     *
     * @param preMuImportLog 涉企数据导入记录
     * @return 涉企数据导入记录
     */
    @Override
    public List<PreMuImportLog> selectPreMuImportLogList(PreMuImportLog preMuImportLog)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        preMuImportLog.setCounty(sysUser.getDistrict());
        preMuImportLog.setDelFlag("0");
        return preMuImportLogMapper.selectPreMuImportLogList(preMuImportLog);
    }

    /**
     * 新增涉企数据导入记录
     *
     * @param preMuImportLog 涉企数据导入记录
     * @return 结果
     */
    @Override
    public int insertPreMuImportLog(PreMuImportLog preMuImportLog)
    {

        return preMuImportLogMapper.insertPreMuImportLog(preMuImportLog);
    }

    /**
     * 修改涉企数据导入记录
     *
     * @param preMuImportLog 涉企数据导入记录
     * @return 结果
     */
    @Override
    public int updatePreMuImportLog(PreMuImportLog preMuImportLog)
    {
        return preMuImportLogMapper.updatePreMuImportLog(preMuImportLog);
    }

    /**
     * 批量删除涉企数据导入记录
     *
     * @param ids 需要删除的涉企数据导入记录主键
     * @return 结果
     */
    @Override
    public int deletePreMuImportLogByIds(String[] ids)
    {
        return preMuImportLogMapper.deletePreMuImportLogByIds(ids);
    }

    /**
     * 删除涉企数据导入记录信息
     *
     * @param id 涉企数据导入记录主键
     * @return 结果
     */
    @Override
    public int deletePreMuImportLogById(String id)
    {
        return preMuImportLogMapper.deletePreMuImportLogById(id);
    }

    @Override
    public void removeData(PreMuImportLog removeParams) {
        preMuImportLogMapper.removeData(removeParams);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeCollectData(PreMuImportLog preMuImportLog) {

        PreMuBenefitEvaluation preMuBenefitEvaluation = new PreMuBenefitEvaluation();
        preMuBenefitEvaluation.setCounty(preMuImportLog.getCounty());
        preMuBenefitEvaluation.setYears(preMuImportLog.getYear());
        preMuBenefitEvaluation.setCompanyType(preMuImportLog.getCompanyType());
        //查询企业ID
        preMuBenefitEvaluationMapper.removeCollectData(preMuBenefitEvaluation);

        PreMuImportAdd preMuImportAdd = new PreMuImportAdd();
        preMuImportAdd.setYear(preMuImportLog.getYear());
        preMuImportAdd.setCounty(preMuImportLog.getCounty());
        preMuImportAdd.setCompanyType(preMuImportLog.getCompanyType());
        preMuImportAddMapper.removeData(preMuImportAdd);

        PreMuImportSubstract preMuImportSubstract = new PreMuImportSubstract();
        preMuImportSubstract.setYear(preMuImportLog.getYear());
        preMuImportSubstract.setCounty(preMuImportLog.getCounty());
        preMuImportSubstract.setCompanyType(preMuImportLog.getCompanyType());
        preMuImportSubstractMapper.removeData(preMuImportSubstract);

        //删除已汇入的不参评企业信息
        preMuBenefitEvaluationNoMapper.removeCollectData(preMuBenefitEvaluation);
    }

    @Override
    public Workbook writeCompanyInfo(PreMuCompanyInfo queryParams, InputStream input) {
        Workbook wb = null;
        try{
            wb = WorkbookFactory.create(input);
            Sheet sheet = wb.getSheetAt(0);
            List<PreMuCompanyInfo> comList = preMuCompanyInfoService.list(queryParams);
            for (int i = 0; i < comList.size(); i++) {
                PreMuCompanyInfo preMuCompanyInfo = comList.get(i);
                Row row = sheet.getRow(i + 1);
                if (row == null) {
                    row = sheet.createRow(i + 1);
                }
                Cell usccCell = row.getCell(0);
                if (usccCell == null) {
                    usccCell = row.createCell(0);
                }
                Cell comNameCell = row.getCell(1);
                if (comNameCell == null) {
                    comNameCell = row.createCell(1);
                }
                usccCell.setCellType(CellType.STRING);
                usccCell.setCellValue(preMuCompanyInfo.getUscc());
                comNameCell.setCellType(CellType.STRING);
                comNameCell.setCellValue(preMuCompanyInfo.getCompanyName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    /**
     * <p>
     *  一键汇总
     * </p>
     *
     * @param params indexWeightMainParams - 指标数据
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult collectData(Map<String, String> params) {
        
        //获取到年份
        String year = params.get("year");
        String companyTypeTemp = params.get("companyType");
        String countyTemp = params.get("county");

        int count = preMuBenefitEvaluationMapper.selectDataCount(year,companyTypeTemp,countyTemp);

        int noCount = preMuBenefitEvaluationNoMapper.selectDataCount(year,companyTypeTemp,countyTemp);

        if((count + noCount) > 0){
            return AjaxResult.error("检测到不参评或者参评的评价结果有数据，请先删除在汇入");
        }

        //查询指标管理中和收入利润率相关的指标

//        PreMuIndexWeightMain weightParamsDto = new PreMuIndexWeightMain();
//        weightParamsDto.setCounty(countyTemp);
//        weightParamsDto.setYear(year);
//        weightParamsDto.setCompanyType(companyTypeTemp);
//        //与利润率比对的值
//        BigDecimal srldbValue = new BigDecimal("-1");
//        BigDecimal srlrvFz = new BigDecimal("0");
//        List<PreMuIndexWeightMain> preMuIndexWeightMains = preMuIndexWeightMainMapper.selectSubList(weightParamsDto);
//        if(preMuIndexWeightMains != null && !preMuIndexWeightMains.isEmpty()){
//            //每一年一个区县一个企业类型，只会有一条数据
//            PreMuIndexWeightMain preMuIndexWeightMain = preMuIndexWeightMains.get(0);
//            List<PreMuIndexWeightSub> preMuIndexWeightSubList = preMuIndexWeightMain.getPreMuIndexWeightSubList();
//            if(preMuIndexWeightSubList != null && !preMuIndexWeightSubList.isEmpty()){
//                //这里的  收入利润率 一定要和指标中配置的一致
//                preMuIndexWeightSubList = preMuIndexWeightSubList.stream().filter(item -> "收入利润率".equals(item.getIndexName())).collect(Collectors.toList());
//                if(preMuIndexWeightSubList != null && !preMuIndexWeightSubList.isEmpty()){
//                    PreMuIndexWeightSub preMuIndexWeightSub = preMuIndexWeightSubList.get(0);
//                    String indexValueLine = preMuIndexWeightSub.getIndexValueLine();
//                    String indexValue = preMuIndexWeightSub.getIndexValue();
//                    srldbValue = new BigDecimal(indexValueLine);
//                    srlrvFz = new BigDecimal(indexValue);
//                }
//            }
//        }

        //统计局
        Map<String, PreMuImportTongji> tongJiMap = new HashMap<String, PreMuImportTongji>(128);
        //税务局
        Map<String, PreMuImportShuiwu> shuiWuMap = new HashMap<String, PreMuImportShuiwu>(128);
        //自然资源和规划局
        Map<String, PreMuImportGuotu> guoTuMap = new HashMap<String, PreMuImportGuotu>(128);
        //生态环境局对象
        Map<String, PreMuImportHuanbao> huanBaoMap = new HashMap<String, PreMuImportHuanbao>(128);
        //人力资源和社会保障局
        Map<String, PreMuImportRenshe> renSheMap = new HashMap<String, PreMuImportRenshe>(128);
        //企业基本信息
        Map<String, PreMuCompanyInfo> comMap = new HashMap<String, PreMuCompanyInfo>(128);

        Map<String, Object> importAddSubstractDOMap = new HashMap<String, Object>();

        //导入的评价结果
        Map<String, Object> benefitEvaluationMap = new HashMap<String, Object>();
        //水利
        Map<String, PreMuImportShuili> shuiLiMap = new HashMap<String, PreMuImportShuili>();

        Map<String, List<String>> addDOExistMap = new HashMap<String, List<String>>();

        Map<String, List<String>> substractDOExistMap = new HashMap<String, List<String>>();

        Map<String, Object> vetoIdMap = new HashMap<String, Object>();

        //查询出统计局已导入的数据
        PreMuImportTongji tongJiQueryParams = new PreMuImportTongji();
        tongJiQueryParams.setYear(year);
        tongJiQueryParams.setCompanyType(params.get("companyType"));
        tongJiQueryParams.setCounty(params.get("county"));
        List<PreMuImportTongji> tongJiList = preMuImportTongjiService.selectPreMuImportTongjiList(tongJiQueryParams);

        //查询税务局数据
        PreMuImportShuiwu shuiWuQueryParam = new PreMuImportShuiwu();
        shuiWuQueryParam.setYear(year);
        shuiWuQueryParam.setCompanyType(params.get("companyType"));
        shuiWuQueryParam.setCounty(params.get("county"));
        List<PreMuImportShuiwu> shuiWuList = preMuImportShuiwuService.selectPreMuImportShuiwuList(shuiWuQueryParam);
        //查询自然资源和规划局
        PreMuImportGuotu guoTuQueryGuoTuParams = new PreMuImportGuotu();
        guoTuQueryGuoTuParams.setYear(year);
        guoTuQueryGuoTuParams.setCompanyType(params.get("companyType"));
        guoTuQueryGuoTuParams.setCounty(params.get("county"));
        List<PreMuImportGuotu> guotuList = preMuImportGuotuService.selectPreMuImportGuotuList(guoTuQueryGuoTuParams);
        //查询人社

        PreMuImportRenshe renSheQueryParams = new PreMuImportRenshe();
        renSheQueryParams.setYear(year);
        renSheQueryParams.setCompanyType(params.get("companyType"));
        renSheQueryParams.setCounty(params.get("county"));
        List<PreMuImportRenshe> renSheList = preMuImportRensheService.selectPreMuImportRensheList(renSheQueryParams);
        //查询环保
        PreMuImportHuanbao huanBaoQueryParams = new PreMuImportHuanbao();
        huanBaoQueryParams.setYear(year);
        huanBaoQueryParams.setCompanyType(params.get("companyType"));
        huanBaoQueryParams.setCounty(params.get("county"));
        List<PreMuImportHuanbao> huanBaoList = preMuImportHuanbaoService.selectPreMuImportHuanbaoList(huanBaoQueryParams);

        //查询水利
        PreMuImportShuili shuiLiQueayParams = new PreMuImportShuili();
        shuiLiQueayParams.setYear(year);
        shuiLiQueayParams.setCompanyType(params.get("companyType"));
        shuiLiQueayParams.setCounty(params.get("county"));
        List<PreMuImportShuili> shuiLiList = preMuImportShuiliService.selectPreMuImportShuiliList(shuiLiQueayParams);
        //评价结果
        PreMuImportBenefitEvaluation pjjgQueryParams = new PreMuImportBenefitEvaluation();
        pjjgQueryParams.setYear(year);
        pjjgQueryParams.setCompanyType(params.get("companyType"));
        pjjgQueryParams.setCounty(params.get("county"));
        List<PreMuImportBenefitEvaluation> preMuImportBenefitEvaluations = preMuImportBenefitEvaluationService.selectPreMuImportBenefitEvaluationList(pjjgQueryParams);

        //查询企业信息表
        PreMuCompanyInfo companyInfoQueryParams = new PreMuCompanyInfo();
        companyInfoQueryParams.setCompanyType(params.get("companyType"));
        companyInfoQueryParams.setCounty(params.get("county"));
//        companyInfoQueryParams.setNum("1");
        companyInfoQueryParams.setYear(year);
        List<PreMuCompanyInfo> comList = preMuCompanyInfoService.list(companyInfoQueryParams);


        //查询加分项，不带年
        PreMuImportAdd addQueryParams = new PreMuImportAdd();
        addQueryParams.setCounty(params.get("county"));
        addQueryParams.setCompanyType(params.get("companyType"));
        List<PreMuImportAdd> addDOExistList = preMuImportAddMapper.list(addQueryParams);

        //查询减分项，不带年
        PreMuImportSubstract subQueryParams = new PreMuImportSubstract();
        subQueryParams.setCounty(params.get("county"));
        subQueryParams.setCompanyType(params.get("companyType"));
        List<PreMuImportSubstract> substractDOExistList = preMuImportSubstractMapper.list(subQueryParams);

        PreMuImportAddSubstract asQueryParams = new PreMuImportAddSubstract();
        asQueryParams.setYear(year);
        asQueryParams.setCounty(params.get("county"));
        asQueryParams.setCompanyType(params.get("companyType"));
        List<PreMuImportAddSubstract> importAddSubstractDOList = preMuImportAddSubstractMapper.list(asQueryParams);

        //否决项
        PreMuImportVeto vetoQueryParams = new PreMuImportVeto();
        vetoQueryParams.setYear(year);
        vetoQueryParams.setCounty(params.get("county"));
        vetoQueryParams.setCompanyType(params.get("companyType"));
        List<Map<String, Object>> votoDOList = preMuImportVetoMapper.list(vetoQueryParams);

        List<PreMuImportAddSubstract> addSaveList = new ArrayList<PreMuImportAddSubstract>();
        List<PreMuImportAddSubstract> addUpdateList = new ArrayList<PreMuImportAddSubstract>();
        List<PreMuImportAddSubstract> substractSaveList = new ArrayList<PreMuImportAddSubstract>();
        List<PreMuImportAddSubstract> substractUpdateList = new ArrayList<PreMuImportAddSubstract>();


        //查询亩产评价表
        PreMuBenefitEvaluation benefitEvaluationQueryParams = new PreMuBenefitEvaluation();
        benefitEvaluationQueryParams.setCounty(params.get("county"));
        benefitEvaluationQueryParams.setCompanyType(params.get("companyType"));
        benefitEvaluationQueryParams.setYears(year);
//        List<PreMuBenefitEvaluation> preMuBenefitEvaluations = preMuBenefitEvaluationMapper.list(benefitEvaluationQueryParams);
        List<PreMuBenefitEvaluation> preMuBenefitEvaluations = preMuBenefitEvaluationMapper.selectUsccAndIdList(benefitEvaluationQueryParams);


        //查询亩产评价非参评的表
        PreMuBenefitEvaluationNo queryNoCp = new PreMuBenefitEvaluationNo();
        queryNoCp.setCounty(params.get("county"));
        queryNoCp.setCompanyType(params.get("companyType"));
        queryNoCp.setYear(year);
        List<PreMuBenefitEvaluationNo> preMuBenefitEvaluationNos = preMuBenefitEvaluationNoMapper.selectPreMuBenefitEvaluationNoList(queryNoCp);

        Map<String, Object> mcImportExistMap = new HashMap<String, Object>();
        Map<String, Object> noCpExistMap = new HashMap<String, Object>();

        if(preMuBenefitEvaluationNos != null && !preMuBenefitEvaluationNos.isEmpty()){
            preMuBenefitEvaluationNos.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    noCpExistMap.put(item.getUscc(), item);
                }
            });
        }

        List<PreMuBenefitEvaluation> addImportList = new ArrayList<PreMuBenefitEvaluation>();
        List<PreMuBenefitEvaluation> updateImportList = new ArrayList<PreMuBenefitEvaluation>();

        //TODO 亩产评价导入
        List<PreMuImportBenefitEvaluation> addImportEvaluationList = new ArrayList<PreMuImportBenefitEvaluation>();
        List<PreMuImportBenefitEvaluation> updateImportEvaluationList = new ArrayList<PreMuImportBenefitEvaluation>();



        //统计局
        if (tongJiList != null && !tongJiList.isEmpty()) {
            tongJiList.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    tongJiMap.put(item.getUscc(), item);
                }
            });
        }

        //评价结果
        if (preMuImportBenefitEvaluations != null && !preMuImportBenefitEvaluations.isEmpty()) {
            preMuImportBenefitEvaluations.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    benefitEvaluationMap.put(item.getUscc(), item);
                }
            });
        }

        //税务局
        if (shuiWuList != null && !shuiWuList.isEmpty()) {
            shuiWuList.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    shuiWuMap.put(item.getUscc(), item);
                }
            });
        }

        //自然资源和规划局
        if (guotuList != null && !guotuList.isEmpty()) {
            guotuList.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    guoTuMap.put(item.getUscc(), item);
                }
            });
        }

        //环保局
        if (huanBaoList != null && !huanBaoList.isEmpty()) {
            huanBaoList.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    huanBaoMap.put(item.getUscc(), item);
                }
            });
        }

        //人社局
        if (renSheList != null && !renSheList.isEmpty()) {
            renSheList.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    renSheMap.put(item.getUscc(), item);
                }
            });
        }

        //水利局
        if (shuiLiList != null && !shuiLiList.isEmpty()) {
            shuiLiList.forEach(item -> {
                if (item.getWaterBureau() != null && !"".equals(item.getWaterBureau())) {
                    shuiLiMap.put(item.getUscc(), item);
                }
            });
        }


        if (comList != null && !comList.isEmpty()) {
            comList.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    comMap.put(item.getUscc(), item);
                }
            });
        }

        //自已新增，不参考亿云代码
        /**
         * 这个Map的主要作用是保存加分项的ID，ID之间通过逗号分割
         * Key是企业的Uscc,值是加分项的ID
         */
        Map<String,String> addIdMap = new HashMap<>(16);

        /**
         * 这个Map的作用是保存加分项的和
         * key是企业的Uscc，值就是分数之和
         */
        Map<String,BigDecimal> addInfoScoreMap = new HashMap<>(16);


        /**
         * 这个Map的主要作用是保存加分项的ID，ID之间通过逗号分割
         * Key是企业的Uscc,值是加分项的ID
         */
        Map<String,String> subIdMap = new HashMap<>(16);

        /**
         * 这个Map的作用是保存加分项的和
         * key是企业的Uscc，值就是分数之和
         */
        Map<String,BigDecimal> subInfoScoreMap = new HashMap<>(16);

        //加载该区县设定的加分项
        //Id和分数的映射
        Map<String, String> addSubItemCountyMap = new HashMap<>(128);
        //加分项ID和父级ID的映射
        Map<String, String> addSubItemCountyIdToParentIdMap = new HashMap<>(128);
        PreMuAddSubstractItem queryPreMuAddSubItem = new PreMuAddSubstractItem();
        queryPreMuAddSubItem.setYear(year);
        queryPreMuAddSubItem.setCounty(params.get("county"));
        queryPreMuAddSubItem.setItemType("1");
        List<PreMuAddSubstractItem> preMuAddSubstractItems = preMuAddSubstractItemMapper.selectPreMuAddSubstractItemList(queryPreMuAddSubItem);
        if(preMuAddSubstractItems != null && !preMuAddSubstractItems.isEmpty()){
            addSubItemCountyMap = preMuAddSubstractItems.stream().collect(Collectors.toMap(PreMuAddSubstractItem::getId, PreMuAddSubstractItem::getItemScore));
            addSubItemCountyIdToParentIdMap = preMuAddSubstractItems.stream().collect(Collectors.toMap(PreMuAddSubstractItem::getId,PreMuAddSubstractItem::getParentId));
        }

        //加减分项
        String oldUscc = "";
        List<PreMuImportAddSubstract> itemList = new ArrayList<PreMuImportAddSubstract>();
        if (importAddSubstractDOList != null && !importAddSubstractDOList.isEmpty()) {
            for (int i = 0; i < importAddSubstractDOList.size(); i++) {
                PreMuImportAddSubstract item = importAddSubstractDOList.get(i);
                String uscc = item.getUscc();
                if ("".equals(oldUscc)) {
                    oldUscc = uscc;
                }
                if (!oldUscc.equals(item.getUscc())) {
                    importAddSubstractDOMap.put(oldUscc, itemList);
                    oldUscc = item.getUscc();
                    itemList = new ArrayList<PreMuImportAddSubstract>();
                }

                //处理加分项
                if(StringUtils.equals(String.valueOf(item.getItemType()),"1")){
                    /**
                     * 记录加分项的Id
                     */
                    if(!addIdMap.containsKey(uscc)){
                        //加分项Id
                        String itemId = item.getItemId();
                        addIdMap.put(uscc,itemId);
                    }else{
                        //已经在加分项IdMap中存在的ID
                        String addId = addIdMap.get(uscc);
                        String newId = StringUtils.join(addId,",",item.getItemId());
                        addIdMap.put(uscc,newId);
                    }

                    /**
                     * 记录加分项的值
                     */
                    if(!addInfoScoreMap.containsKey(uscc)){
                        String itemId = item.getItemId();
                        //获取出加分项的父级Id
                        String parentId = addSubItemCountyIdToParentIdMap.get(itemId);
                        if(StringUtils.equals(parentId,"0")){
                            BigDecimal itemScore = item.getItemScore() == null ? BigDecimal.ZERO : item.getItemScore();
                            addInfoScoreMap.put(uscc,itemScore);
                        }else{
                            //获取父级Id的分数
                            String parentScore = addSubItemCountyMap.get(parentId);
                            BigDecimal parentScoreBigDecimal = new BigDecimal("0");
                            if(StringUtils.isNotBlank(parentScore)){
                                parentScoreBigDecimal = new BigDecimal(parentScore);
                            }
                            BigDecimal itemScore = item.getItemScore() == null ? BigDecimal.ZERO : item.getItemScore();
                            if(itemScore.compareTo(parentScoreBigDecimal) > 0){
                                itemScore = parentScoreBigDecimal;
                            }

                            addInfoScoreMap.put(uscc,itemScore);
                        }

                    }else{
                        //如果企业的加分项已经存在。则将旧的值取出，加上新的值重新保存到Map中
                        BigDecimal bigDecimal = addInfoScoreMap.get(uscc);
                        String itemId = item.getItemId();
                        //获取出加分项的父级Id
                        String parentId = addSubItemCountyIdToParentIdMap.get(itemId);
                        if(StringUtils.equals(parentId,"0")){
                            BigDecimal itemScore = item.getItemScore() == null ? BigDecimal.ZERO : item.getItemScore();
                            addInfoScoreMap.put(uscc,bigDecimal.add(itemScore));
                        }else{
                            //获取父级Id的分数
                            String parentScore = addSubItemCountyMap.get(parentId);
                            BigDecimal parentScoreBigDecimal = new BigDecimal("0");
                            if(StringUtils.isNotBlank(parentScore)){
                                parentScoreBigDecimal = new BigDecimal(parentScore);
                            }
//                        BigDecimal newBigDecimal = item.getItemScore() == null ? BigDecimal.ZERO : bigDecimal.add(item.getItemScore());
                            BigDecimal newBigDecimal = item.getItemScore() == null ? BigDecimal.ZERO : item.getItemScore();
                            if(newBigDecimal.compareTo(parentScoreBigDecimal) > 0){
                                newBigDecimal = parentScoreBigDecimal;
                            }
                            addInfoScoreMap.put(uscc,bigDecimal.add(newBigDecimal));
                        }

                    }
                }

                if(StringUtils.equals(String.valueOf(item.getItemType()),"2")){
                    //记录减分项的ID
                    if(!subIdMap.containsKey(uscc)){
                        //减分项Id
                        String itemId = item.getItemId();
                        subIdMap.put(uscc,itemId);
                    }else{
                        //已经在减分项IdMap中存在的ID
                        String addId = subIdMap.get(uscc);
                        String newId = StringUtils.join(addId,",",item.getItemId());
                        subIdMap.put(uscc,newId);
                    }

                    /**
                     * 记录减分项的值
                     */
                    if(!subInfoScoreMap.containsKey(uscc)){
                        BigDecimal itemScore = item.getItemScore() == null ? BigDecimal.ZERO : item.getItemScore();
                        subInfoScoreMap.put(uscc,itemScore);
                    }else{
                        //如果企业的减分项已经存在。则将旧的值取出，加上新的值重新保存到Map中
                        BigDecimal bigDecimal = subInfoScoreMap.get(uscc);
                        BigDecimal newBigDecimal = item.getItemScore() == null ? BigDecimal.ZERO : bigDecimal.add(item.getItemScore());
                        subInfoScoreMap.put(uscc,newBigDecimal);
                    }
                }
                
                
                itemList.add(item);
            }
            importAddSubstractDOMap.put(oldUscc, itemList);
        }

        oldUscc = "";
        List<String> itemIdList = new ArrayList<String>();
        if (addDOExistList != null && !addDOExistList.isEmpty()) {
//            for (int i = 0; i < addDOExistList.size(); i++) {
//                PreMuImportAdd item = addDOExistList.get(i);
//                if ("".equals(oldUscc)) {
//                    oldUscc = item.getUscc();
//                }
//                if (!oldUscc.equals(item.getUscc())) {
//                    addDOExistMap.put(oldUscc, itemIdList);
//                    oldUscc = item.getUscc();
//                    itemIdList = new ArrayList<String>();
//                }
//                itemIdList.add(item.getItemId());
//            }
            addDOExistMap = addDOExistList.stream().collect(Collectors.groupingBy(PreMuImportAdd::getUscc,Collectors.mapping(PreMuImportAdd::getItemId, Collectors.toList())));
        }

        oldUscc = "";
        itemIdList = new ArrayList<String>();
        if (substractDOExistList != null && !substractDOExistList.isEmpty()) {
//            for (int i = 0; i < substractDOExistList.size(); i++) {
//                PreMuImportSubstract item = substractDOExistList.get(i);
//                if ("".equals(oldUscc)) {
//                    oldUscc = item.getUscc();
//                }
//                if (!oldUscc.equals(item.getUscc())) {
//                    substractDOExistMap.put(oldUscc, itemIdList);
//                    oldUscc = item.getUscc();
//                    itemIdList = new ArrayList<String>();
//                }
//                itemIdList.add(item.getItemId());
//            }
//            substractDOExistMap.put(oldUscc, itemIdList);
            substractDOExistMap = substractDOExistList.stream().collect(Collectors.groupingBy(PreMuImportSubstract::getUscc,Collectors.mapping(PreMuImportSubstract::getItemId, Collectors.toList())));
        }

        //否决项
        if (votoDOList != null && !votoDOList.isEmpty()) {
            votoDOList.forEach(item -> {
                if (item.get("uscc") != null && !"".equals(item.get("uscc"))) {
                    vetoIdMap.put((String) item.get("uscc"), item.get("ids"));
                }
            });
        }

        if (preMuBenefitEvaluations != null && !preMuBenefitEvaluations.isEmpty()) {
            preMuBenefitEvaluations.forEach(item -> {
                if (item.getUscc() != null && !"".equals(item.getUscc())) {
                    mcImportExistMap.put(item.getUscc(), item.getId());
                }
            });
        }

        //指标权重检索
        if (params.get("sign") == null || "".equals(params.get("sign"))) {
            //TODO 添加指标权重
            boolean correctFlag = true;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < tongJiList.size(); i++) {
                PreMuImportTongji mcImportTongjiDO = tongJiList.get(i);

                PreMuBenefitEvaluation preMuBenefitEvaluation = new PreMuBenefitEvaluation();
                PreMuCompanyInfo preMuCompanyInfo = comMap.get(mcImportTongjiDO.getUscc());
                String companyId = preMuCompanyInfo.getId();

                //企业的USCC
                String uscc = mcImportTongjiDO.getUscc();

                PreMuImportShuiwu preMuImportShuiwu = shuiWuMap.get(mcImportTongjiDO.getUscc());
                PreMuImportGuotu preMuImportGuotu = guoTuMap.get(mcImportTongjiDO.getUscc());
                PreMuImportHuanbao preMuImportHuanbao = huanBaoMap.get(mcImportTongjiDO.getUscc());
                PreMuImportRenshe preMuImportRenshe = renSheMap.get(mcImportTongjiDO.getUscc());
                PreMuImportShuili preMuImportShuili = shuiLiMap.get(mcImportTongjiDO.getUscc());
                if (preMuImportShuiwu == null) {
                    preMuImportShuiwu = new PreMuImportShuiwu();
                }
                if (preMuImportGuotu == null) {
                    preMuImportGuotu = new PreMuImportGuotu();
                }
                if (preMuImportHuanbao == null) {
                    preMuImportHuanbao = new PreMuImportHuanbao();
                }
                if (preMuImportRenshe == null) {
                    preMuImportRenshe = new PreMuImportRenshe();
                }
                if (preMuImportShuili == null){
                    preMuImportShuili = new PreMuImportShuili();
                }

                List<PreMuImportAddSubstract> miasDOs = (List<PreMuImportAddSubstract>)importAddSubstractDOMap.get(mcImportTongjiDO.getUscc());

                //当前企业已经存在的加减分项
                List<String> addDOs = (List<String>)addDOExistMap.get(mcImportTongjiDO.getUscc());
                List<String> substractDOs =  (List<String>)substractDOExistMap.get(mcImportTongjiDO.getUscc());

                if (miasDOs != null) {
                    for (PreMuImportAddSubstract miasDO : miasDOs) {
                        if (miasDO.getItemType() == 1L) {
                            if (addDOs != null && addDOs.contains(miasDO.getItemId())) {
                                miasDO.setDelFlag("0");
                                addUpdateList.add(miasDO);
                            } else {
                                miasDO.setId(UUID.randomUUID().toString().replace("-", ""));
                                miasDO.setDelFlag("0");
                                addSaveList.add(miasDO);
                            }
                        }

                        if (miasDO.getItemType() == 2L) {
                            if (substractDOs != null && substractDOs.contains(miasDO.getItemId())) {
                                miasDO.setDelFlag("0");
                                substractUpdateList.add(miasDO);
                            } else {
                                miasDO.setId(UUID.randomUUID().toString().replace("-", ""));
                                miasDO.setDelFlag("0");
                                substractSaveList.add(miasDO);
                            }
                        }

                    }

                    if (addSaveList != null && addSaveList.size() >= BATCH_SIZE) {
                        preMuImportAddMapper.batchSave(addSaveList);
                        addSaveList.clear();
                        System.gc();
                    }
                    if (addUpdateList != null && addUpdateList.size() >= BATCH_SIZE) {
                        preMuImportAddMapper.batchUpdate(addUpdateList);
                        addUpdateList.clear();
                        System.gc();
                    }

                    if (substractSaveList != null && substractSaveList.size() >= BATCH_SIZE) {
                        preMuImportSubstractMapper.batchSave(substractSaveList);
                        substractSaveList.clear();
                        System.gc();
                    }
                    if (substractUpdateList != null && substractUpdateList.size() >= BATCH_SIZE) {
                        preMuImportSubstractMapper.batchUpdate(substractUpdateList);
                        substractUpdateList.clear();
                        System.gc();
                    }
                }
                //否决项
                String vetoIds = (String) vetoIdMap.get(mcImportTongjiDO.getUscc());
                Map<String, Object> mcBenefitEvaluationMap = new HashMap<String, Object>();
                mcBenefitEvaluationMap.put("year", year);
                mcBenefitEvaluationMap.put("companyId", companyId);

                if (mcImportExistMap == null || mcImportExistMap.isEmpty() || !mcImportExistMap.containsKey(mcImportTongjiDO.getUscc())) {
                    preMuBenefitEvaluation.setId(UUID.randomUUID().toString().replace("-", ""));

                    //写入该企业的加分项的ID信息
                    String currentEnterAddIds = addIdMap.get(uscc);
                    preMuBenefitEvaluation.setAddItemIds(currentEnterAddIds);
                    preMuBenefitEvaluation.setAddScore(addInfoScoreMap.get(uscc));

                    //写入该企业的减分项
                    String subIds = subIdMap.get(uscc);
                    preMuBenefitEvaluation.setSubstractItemIds(subIds);
                    preMuBenefitEvaluation.setSubScore(subInfoScoreMap.get(uscc));


                    preMuBenefitEvaluation.setYears(year);
                    preMuBenefitEvaluation.setCompanyId(companyId);
                    preMuBenefitEvaluation.setModuleType(preMuCompanyInfo.getModuleType());
                    preMuBenefitEvaluation.setCompanyName(preMuCompanyInfo.getCompanyName());
                    preMuBenefitEvaluation.setCompanyProp(mcImportTongjiDO.getCompanyProp());
                    preMuBenefitEvaluation.setCompanyType(mcImportTongjiDO.getCompanyType());
                    preMuBenefitEvaluation.setCompanyScale(mcImportTongjiDO.getCompanyScale());
                    //preMuBenefitEvaluation.setIndustryType(mcImportTongjiDO.getIndustryType());
                    //preMuBenefitEvaluation.setIndustryMediumType(mcImportTongjiDO.getIndustryMediumType());
                    PreMuCompanyInfo companyInfo = comMap.get(uscc);
                    if(companyInfo != null){
                        preMuBenefitEvaluation.setIndustryType(companyInfo.getIndustryType());
                        preMuBenefitEvaluation.setIndustryMediumType(companyInfo.getIndustryMediumType());
                    }
                    preMuBenefitEvaluation.setAddr(mcImportTongjiDO.getAddr());

                    preMuBenefitEvaluation.setActualTax(preMuImportShuiwu.getActualTax() == null ? null : new BigDecimal(preMuImportShuiwu.getActualTax()));
                    preMuBenefitEvaluation.setRentOutArea(preMuImportGuotu.getRentOutArea());
                    preMuBenefitEvaluation.setUseArea(preMuImportGuotu.getUseArea());
                    preMuBenefitEvaluation.setLeasedArea(preMuImportGuotu.getLeasedArea());
                    preMuBenefitEvaluation.setOtherArea(preMuImportGuotu.getOtherArea());
                    preMuBenefitEvaluation.setSumArea(preMuImportGuotu.getSumArea());


                    preMuBenefitEvaluation.setWaterBureau(preMuImportShuili.getWaterBureau()); //添加耗水量
                    preMuBenefitEvaluation.setAddedTax(preMuImportShuiwu.getAddedTax() == null ? null : new BigDecimal(preMuImportShuiwu.getAddedTax()));

                    preMuBenefitEvaluation.setExciseTax(preMuImportShuiwu.getExciseTax() == null ? null : new BigDecimal(preMuImportShuiwu.getExciseTax()));
                    preMuBenefitEvaluation.setProfitTotal(preMuImportShuiwu.getProfitTotal() == null ? null : new BigDecimal(preMuImportShuiwu.getProfitTotal()));
                    preMuBenefitEvaluation.setCompanyTax(preMuImportShuiwu.getCompanyTax() == null ? null : new BigDecimal(preMuImportShuiwu.getCompanyTax()));
                    preMuBenefitEvaluation.setPersonalTax(preMuImportShuiwu.getPersonalTax() == null ? null : new BigDecimal(preMuImportShuiwu.getPersonalTax()));
                    preMuBenefitEvaluation.setResourcesTax(preMuImportShuiwu.getResourcesTax() == null ? null : new BigDecimal(preMuImportShuiwu.getResourcesTax()));
                    preMuBenefitEvaluation.setCityMaintainTax(preMuImportShuiwu.getCityMaintainTax() == null ? null : new BigDecimal(preMuImportShuiwu.getCityMaintainTax()));
                    preMuBenefitEvaluation.setRealestateTax(preMuImportShuiwu.getRealestateTax() == null ? null : new BigDecimal(preMuImportShuiwu.getRealestateTax()));
                    System.out.println(preMuImportShuiwu.getCompanyName()+"-----"+preMuImportShuiwu.getUscc());
                    preMuBenefitEvaluation.setPrintingTax(preMuImportShuiwu.getPrintingTax() == null ? null : new BigDecimal(preMuImportShuiwu.getPrintingTax()));
                    preMuBenefitEvaluation.setLandTax(preMuImportShuiwu.getLandTax() == null ? null : new BigDecimal(preMuImportShuiwu.getLandTax()));
                    preMuBenefitEvaluation.setLandAppreciationTax(preMuImportShuiwu.getLandAppreciationTax() == null ? null : new BigDecimal(preMuImportShuiwu.getLandAppreciationTax()));
                    preMuBenefitEvaluation.setTravelTax(preMuImportShuiwu.getTravelTax() == null ? null : new BigDecimal(preMuImportShuiwu.getTravelTax()));
                    preMuBenefitEvaluation.setVehiclePurchaseTax(preMuImportShuiwu.getVehiclePurchaseTax() == null ? null : new BigDecimal(preMuImportShuiwu.getVehiclePurchaseTax()));
                    preMuBenefitEvaluation.setFarmlandOccupationTax(preMuImportShuiwu.getFarmlandOccupationTax() == null ? null : new BigDecimal(preMuImportShuiwu.getFarmlandOccupationTax()));
                    preMuBenefitEvaluation.setDeedTax(preMuImportShuiwu.getDeedTax() == null ? null : new BigDecimal(preMuImportShuiwu.getDeedTax()));
                    preMuBenefitEvaluation.setEnvironmentalProtectionTax(preMuImportShuiwu.getEnvironmentalProtectionTax() == null ? null : new BigDecimal(preMuImportShuiwu.getEnvironmentalProtectionTax()));
                    preMuBenefitEvaluation.setTobaccoTax(preMuImportShuiwu.getTobaccoTax() == null ? null : new BigDecimal(preMuImportShuiwu.getTobaccoTax()));

                    preMuBenefitEvaluation.setMainIncome(preMuImportShuiwu.getMainIncome() == null ? null : new BigDecimal(preMuImportShuiwu.getMainIncome()));
                    preMuBenefitEvaluation.setCoalConsumption(preMuImportShuiwu.getCoalConsumption() == null ? null :new BigDecimal(preMuImportShuiwu.getCoalConsumption()));
                    preMuBenefitEvaluation.setPowerConsumption(preMuImportShuiwu.getPowerConsumption() == null ? null : new BigDecimal(preMuImportShuiwu.getPowerConsumption()));
                    preMuBenefitEvaluation.setGasConsumption(preMuImportShuiwu.getGasConsumption() == null ? null : new BigDecimal(preMuImportShuiwu.getGasConsumption()));
                    preMuBenefitEvaluation.setTotalUsed(mcImportTongjiDO.getTotalUsed());
                    preMuBenefitEvaluation.setO2(preMuImportHuanbao.getO2());
                    preMuBenefitEvaluation.setAmmonia(preMuImportHuanbao.getAmmonia());
                    preMuBenefitEvaluation.setSo2(preMuImportHuanbao.getSo2());
                    preMuBenefitEvaluation.setNox(preMuImportHuanbao.getNox());

                    preMuBenefitEvaluation.setTotalEquivalent(preMuImportHuanbao.getTotalEquivalent());

                    preMuBenefitEvaluation.setRdFunds(preMuImportShuiwu.getRdFunds() == null? null : new BigDecimal(preMuImportShuiwu.getRdFunds()));

                    if (mcImportTongjiDO.getIndustryOutput() != null && Double.valueOf(mcImportTongjiDO.getIndustryOutput()) != 0) {
                        preMuBenefitEvaluation.setIndustryOutput(mcImportTongjiDO.getIndustryOutput() == null ? null : new BigDecimal(mcImportTongjiDO.getIndustryOutput()));
                    } else {
                        preMuBenefitEvaluation.setIndustryOutput(preMuImportShuiwu.getMainIncome() == null ? null : new BigDecimal(preMuImportShuiwu.getMainIncome()));
                    }

                    preMuBenefitEvaluation.setAnnualEmployees(preMuImportRenshe.getAnnualEmployees());
                    preMuBenefitEvaluation.setStatus("0");
                    preMuBenefitEvaluation.setVetoItemIds(vetoIds);

                    preMuBenefitEvaluation.setDelFlag("0");

                    //计算单位用地税收得分
//                    if(StringUtils.isBlank(preMuImportShuiwu.getActualTax())){
//                        throw new RuntimeException(String.format("%s税收不能为空,请检查!",mcImportTongjiDO.getCompanyName()));
//                    }
//                    BigDecimal sssjgx = preMuImportShuiwu.getActualTax() == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getActualTax());
//
//                    BigDecimal ydmj = preMuImportGuotu.getSumArea() == null ? new BigDecimal("0") : preMuImportGuotu.getSumArea();
//                    if(!(ydmj.compareTo(new BigDecimal(0)) <= 0)){
//                        preMuBenefitEvaluation.setScore1(sssjgx.divide(ydmj,2,BigDecimal.ROUND_HALF_UP));
//                    }else{
//                        throw new RuntimeException(String.format("%s用地面积数据不全,请检查!",mcImportTongjiDO.getCompanyName()));
//                    }

                    //销售收入
                    if(StringUtils.isBlank(preMuImportShuiwu.getMainIncome())){
                        //throw new RuntimeException(String.format("%s营业收入为空,请检查!",mcImportTongjiDO.getCompanyName()));
                        correctFlag = false;
                        sb.append(String.format("%s营业收入为空,请检查!",mcImportTongjiDO.getCompanyName())).append("<br>");
                        continue;
                    }
                    BigDecimal xssr = preMuImportShuiwu.getMainIncome()  == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getMainIncome());


                    //计算单位用地销售收入得分
                    BigDecimal zydmj = preMuImportGuotu.getSumArea() == null ? new BigDecimal("0") : preMuImportGuotu.getSumArea();
                    if(!(zydmj.compareTo(new BigDecimal(0)) == 0)){
                        preMuBenefitEvaluation.setScore7(xssr.divide(zydmj,2,BigDecimal.ROUND_HALF_UP));
                    }

                    //收入利润率(利润总额 ÷ 销售收入)x100%
                    BigDecimal totalProfitBigDecimal = preMuImportShuiwu.getProfitTotal() == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getProfitTotal());
//                    if(totalProfitBigDecimal.compareTo(BigDecimal.ZERO) <= 0){
//                        throw new RuntimeException(String.format("%s利润总额为空或为0,请检查!",mcImportTongjiDO.getCompanyName()));
//                    }
                    if(!(totalProfitBigDecimal.compareTo(new BigDecimal(0)) == 0)){
                        preMuBenefitEvaluation.setScore8(totalProfitBigDecimal.divide(xssr,4,BigDecimal.ROUND_HALF_UP));
                    }else{
                        preMuBenefitEvaluation.setScore8(BigDecimal.ZERO);
                    }
//                    if(!(totalProfitBigDecimal.compareTo(new BigDecimal(0)) == 0)){
//                        BigDecimal valueResult = totalProfitBigDecimal.multiply(BigDecimal.valueOf(100L)).divide(xssr, 2, BigDecimal.ROUND_HALF_UP);
//                        //计算 利润率的值 比基准确高或者低
//                        if(srldbValue.compareTo(new BigDecimal("-1")) == 0){
//                            throw new RuntimeException("利润率的指标没有设置");
//                        }else{
//                            BigDecimal bjjgBigDecimal = srldbValue.subtract(valueResult).multiply(new BigDecimal("100")).divide(srldbValue, 2, BigDecimal.ROUND_HALF_UP);
//                            //这个分支代表 利润率的结果比基准值大  或者与基准值相等
//                            if(bjjgBigDecimal.compareTo(BigDecimal.ZERO) <= 0){
//                                //如果分值大比基准值  分数应为设置的分值
//                                preMuBenefitEvaluation.setScore8(srlrvFz);
//                            } else if (bjjgBigDecimal.compareTo(new BigDecimal(5)) <= 0 && bjjgBigDecimal.compareTo(BigDecimal.ZERO) > 0) {
//                                //百分之80
//                                BigDecimal multiply = valueResult.multiply(new BigDecimal("0.8"));
//                                preMuBenefitEvaluation.setScore8(multiply);
//                            }else if (bjjgBigDecimal.compareTo(new BigDecimal("5")) > 0 && bjjgBigDecimal.compareTo(new BigDecimal("10")) <= 0) {
//                                //百分之60
//                                BigDecimal multiply = valueResult.multiply(new BigDecimal("0.6"));
//                                preMuBenefitEvaluation.setScore8(multiply);
//                            }else if (bjjgBigDecimal.compareTo(new BigDecimal("10")) > 0 && bjjgBigDecimal.compareTo(new BigDecimal("20")) <= 0) {
//                                //百分之40
//                                BigDecimal multiply = valueResult.multiply(new BigDecimal("0.4"));
//                                preMuBenefitEvaluation.setScore8(multiply);
//                            }else if (bjjgBigDecimal.compareTo(new BigDecimal("20")) > 0) {
//                                preMuBenefitEvaluation.setScore8(BigDecimal.ZERO);
//                            }
//                        }
////                        preMuBenefitEvaluation.setScore8(totalProfitBigDecimal.divide(xssr,2,BigDecimal.ROUND_HALF_UP));
//                    }else{
//                        preMuBenefitEvaluation.setScore8(BigDecimal.ZERO);
//                    }

                    if("1".equals(mcImportTongjiDO.getCompanyType())){


                        //计算单位能耗销售收入得分 - 公式：销售收入 / 总能耗
                        //总能耗
                        BigDecimal totalUsedBigDecimal = mcImportTongjiDO.getTotalUsed() == null ? new BigDecimal("0") : new BigDecimal(mcImportTongjiDO.getTotalUsed());
                        if(totalUsedBigDecimal.compareTo(BigDecimal.ZERO) <= 0){
                            //throw new RuntimeException(String.format("%s总能耗为空或为0,请检查!",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s总能耗为空或为0,请检查!",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        if(!(totalUsedBigDecimal.compareTo(new BigDecimal(0)) == 0)){
                            preMuBenefitEvaluation.setScore2(xssr.divide(totalUsedBigDecimal,2,BigDecimal.ROUND_HALF_UP));
                        }

                        //计算单位污染物排放销售收入得分 - 公式：销售收入 / 主要污染排放总当量
                        if(StringUtils.isBlank(preMuImportHuanbao.getTotalEquivalent())){
                            //throw new RuntimeException(String.format("%s 主要污染物排放总当量为空，请检查！",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s 主要污染物排放总当量为空，请检查！",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        BigDecimal wrwpfdl = preMuImportHuanbao.getTotalEquivalent() == null ? new BigDecimal("0") : new BigDecimal(preMuImportHuanbao.getTotalEquivalent());
                        if(!(wrwpfdl.compareTo(new BigDecimal(0)) <= 0)){
                            preMuBenefitEvaluation.setScore3(xssr.divide(wrwpfdl,2,BigDecimal.ROUND_HALF_UP));
                        }else{
                            preMuBenefitEvaluation.setScore3(new BigDecimal("0.0000"));
                        }

                        //计算研发经费投入强度得分 - 公式：研发经费投入强度 = 研发经费支出 / 销售收入
                        if(StringUtils.isBlank(preMuImportShuiwu.getRdFunds())){
                            //throw new RuntimeException(String.format("%s 研发经费投入强度为空，请检查！",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s 研发经费投入强度为空，请检查！",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        BigDecimal yfjfzc = preMuImportShuiwu.getRdFunds() == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getRdFunds());
                        BigDecimal score4 = new BigDecimal("0.00");

                        if(xssr != null && xssr.compareTo(BigDecimal.ZERO) > 0){
                            score4 = yfjfzc.divide(xssr, 4, BigDecimal.ROUND_HALF_UP);
                        }
                        BigDecimal bigDecimal = new BigDecimal("100.0000");
                        score4 = score4.multiply(bigDecimal).setScale(4, BigDecimal.ROUND_HALF_UP);
                        preMuBenefitEvaluation.setScore4(score4);

                        //指标：全员劳动生产率 单位：万元/人   计算方式：工业总产值（现价）/年平均职工人数
                        if(StringUtils.isBlank(mcImportTongjiDO.getIndustryOutput())){
                            //throw new RuntimeException(String.format("%s 工业总产值为空，请检查！",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s 工业总产值为空，请检查！",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        Long annualEmployees = preMuImportRenshe.getAnnualEmployees();
                        if(annualEmployees == null || annualEmployees == 0L){
                            //throw new RuntimeException(String.format("%s 年平均职工人数为空，请检查！",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s 年平均职工人数为空，请检查！",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        BigDecimal gyzcz = mcImportTongjiDO.getIndustryOutput() == null ? new BigDecimal("0") : new BigDecimal(mcImportTongjiDO.getIndustryOutput());
                        BigDecimal score5 = gyzcz.divide(BigDecimal.valueOf(annualEmployees), 2, BigDecimal.ROUND_HALF_UP);
                        preMuBenefitEvaluation.setScore5(score5);

                        //指标：单位水耗销售收入 单位：立方米/万元  计算方式：销售收入（万元）/耗水量
                        BigDecimal waterBureau = preMuImportShuili.getWaterBureau();
                        if (waterBureau != null && waterBureau.compareTo(BigDecimal.ZERO) != 0) {
                            BigDecimal score6 = xssr.divide(waterBureau, 2, BigDecimal.ROUND_HALF_UP);
                            score6 = score6.setScale(2, BigDecimal.ROUND_HALF_UP);
                            preMuBenefitEvaluation.setScore6(score6);
                        } else {
                            preMuBenefitEvaluation.setScore6(new BigDecimal("0.0000"));
                        }
                    }else{
                        BigDecimal scoreNull = new BigDecimal("0.0000");
                        preMuBenefitEvaluation.setScore2(scoreNull);
                        preMuBenefitEvaluation.setScore3(scoreNull);
                        preMuBenefitEvaluation.setScore4(scoreNull);
                        preMuBenefitEvaluation.setScore5(scoreNull);
                        preMuBenefitEvaluation.setScore6(scoreNull);
                    }
                    addImportList.add(preMuBenefitEvaluation);
                }else{
                    preMuBenefitEvaluation.setId((String) mcImportExistMap.get(mcImportTongjiDO.getUscc()));

                    //写入该企业的加分项的ID信息
                    String currentEnterAddIds = addIdMap.get(uscc);
                    preMuBenefitEvaluation.setAddItemIds(currentEnterAddIds);
                    preMuBenefitEvaluation.setAddScore(addInfoScoreMap.get(uscc));

                    //写入该企业的减分项
                    String subIds = subIdMap.get(uscc);
                    preMuBenefitEvaluation.setSubstractItemIds(subIds);
                    preMuBenefitEvaluation.setSubScore(subInfoScoreMap.get(uscc));

                    preMuBenefitEvaluation.setYears(year);
                    preMuBenefitEvaluation.setCompanyId(companyId);
                    preMuBenefitEvaluation.setModuleType(preMuCompanyInfo.getModuleType());
                    preMuBenefitEvaluation.setCompanyName(preMuCompanyInfo.getCompanyName());
                    preMuBenefitEvaluation.setCompanyProp(mcImportTongjiDO.getCompanyProp());
                    preMuBenefitEvaluation.setCompanyType(mcImportTongjiDO.getCompanyType());
                    preMuBenefitEvaluation.setCompanyScale(mcImportTongjiDO.getCompanyScale());
//                    preMuBenefitEvaluation.setIndustryType(mcImportTongjiDO.getIndustryType());
//                    preMuBenefitEvaluation.setIndustryMediumType(mcImportTongjiDO.getIndustryMediumType());
                    PreMuCompanyInfo companyInfo = comMap.get(uscc);
                    if(companyInfo != null){
                        preMuBenefitEvaluation.setIndustryType(companyInfo.getIndustryType());
                        preMuBenefitEvaluation.setIndustryMediumType(companyInfo.getIndustryMediumType());
                    }
                    preMuBenefitEvaluation.setAddr(mcImportTongjiDO.getAddr());


                    preMuBenefitEvaluation.setActualTax(preMuImportShuiwu.getActualTax() == null ? null : new BigDecimal(preMuImportShuiwu.getActualTax()));
                    preMuBenefitEvaluation.setRentOutArea(preMuImportGuotu.getRentOutArea());
                    preMuBenefitEvaluation.setUseArea(preMuImportGuotu.getUseArea());
                    preMuBenefitEvaluation.setLeasedArea(preMuImportGuotu.getLeasedArea());
                    preMuBenefitEvaluation.setOtherArea(preMuImportGuotu.getOtherArea());
                    preMuBenefitEvaluation.setSumArea(preMuImportGuotu.getSumArea());

                    preMuBenefitEvaluation.setWaterBureau(preMuImportShuili.getWaterBureau()); //添加耗水量

                    preMuBenefitEvaluation.setProfitTotal(preMuImportShuiwu.getProfitTotal() == null ? null : new BigDecimal(preMuImportShuiwu.getProfitTotal()));
                    preMuBenefitEvaluation.setAddedTax(preMuImportShuiwu.getAddedTax() == null ? null : new BigDecimal(preMuImportShuiwu.getAddedTax()));
                    preMuBenefitEvaluation.setExciseTax(preMuImportShuiwu.getExciseTax() == null ? null : new BigDecimal(preMuImportShuiwu.getExciseTax()));
                    preMuBenefitEvaluation.setCompanyTax(preMuImportShuiwu.getCompanyTax() == null ? null : new BigDecimal(preMuImportShuiwu.getCompanyTax()));
                    preMuBenefitEvaluation.setPersonalTax(preMuImportShuiwu.getPersonalTax() == null ? null : new BigDecimal(preMuImportShuiwu.getPersonalTax()));
                    preMuBenefitEvaluation.setResourcesTax(preMuImportShuiwu.getResourcesTax() == null ? null : new BigDecimal(preMuImportShuiwu.getResourcesTax()));
                    preMuBenefitEvaluation.setCityMaintainTax(preMuImportShuiwu.getCityMaintainTax() == null ? null : new BigDecimal(preMuImportShuiwu.getCityMaintainTax()));
                    preMuBenefitEvaluation.setRealestateTax(preMuImportShuiwu.getRealestateTax() == null ? null : new BigDecimal(preMuImportShuiwu.getRealestateTax()));
                    preMuBenefitEvaluation.setPrintingTax(preMuImportShuiwu.getPrintingTax() == null ? null : new BigDecimal(preMuImportShuiwu.getPrintingTax()));
                    preMuBenefitEvaluation.setLandTax(preMuImportShuiwu.getLandTax() == null ? null : new BigDecimal(preMuImportShuiwu.getLandTax()));
                    preMuBenefitEvaluation.setLandAppreciationTax(preMuImportShuiwu.getLandAppreciationTax() == null ? null : new BigDecimal(preMuImportShuiwu.getLandAppreciationTax()));
                    preMuBenefitEvaluation.setTravelTax(preMuImportShuiwu.getTravelTax() == null ? null : new BigDecimal(preMuImportShuiwu.getTravelTax()));
                    preMuBenefitEvaluation.setVehiclePurchaseTax(preMuImportShuiwu.getVehiclePurchaseTax() == null ? null: new BigDecimal(preMuImportShuiwu.getVehiclePurchaseTax()));
                    preMuBenefitEvaluation.setFarmlandOccupationTax(preMuImportShuiwu.getFarmlandOccupationTax() == null ? null : new BigDecimal(preMuImportShuiwu.getFarmlandOccupationTax()));
                    preMuBenefitEvaluation.setDeedTax(preMuImportShuiwu.getDeedTax() == null ? null : new BigDecimal(preMuImportShuiwu.getDeedTax()));
                    preMuBenefitEvaluation.setEnvironmentalProtectionTax(preMuImportShuiwu.getEnvironmentalProtectionTax() == null ?  null: new BigDecimal(preMuImportShuiwu.getEnvironmentalProtectionTax()));
                    preMuBenefitEvaluation.setTobaccoTax(preMuImportShuiwu.getTobaccoTax() == null ? null : new BigDecimal(preMuImportShuiwu.getTobaccoTax()));

                    preMuBenefitEvaluation.setMainIncome(preMuImportShuiwu.getMainIncome() == null ? null : new BigDecimal(preMuImportShuiwu.getMainIncome()));
                    preMuBenefitEvaluation.setCoalConsumption(preMuImportShuiwu.getCoalConsumption() == null ? null : new BigDecimal(preMuImportShuiwu.getCoalConsumption()));
                    preMuBenefitEvaluation.setPowerConsumption(preMuImportShuiwu.getPowerConsumption() == null ? null:new BigDecimal(preMuImportShuiwu.getPowerConsumption()));
                    preMuBenefitEvaluation.setGasConsumption(preMuImportShuiwu.getGasConsumption() == null ? null : new BigDecimal(preMuImportShuiwu.getGasConsumption()));
                    preMuBenefitEvaluation.setTotalUsed(mcImportTongjiDO.getTotalUsed());
                    preMuBenefitEvaluation.setO2(preMuImportHuanbao.getO2());
                    preMuBenefitEvaluation.setAmmonia(preMuImportHuanbao.getAmmonia());
                    preMuBenefitEvaluation.setSo2(preMuImportHuanbao.getSo2());
                    preMuBenefitEvaluation.setNox(preMuImportHuanbao.getNox());

                    preMuBenefitEvaluation.setTotalEquivalent(preMuImportHuanbao.getTotalEquivalent());

                    preMuBenefitEvaluation.setRdFunds(preMuImportShuiwu.getRdFunds() == null ? null : new BigDecimal(preMuImportShuiwu.getRdFunds()));

                    if (mcImportTongjiDO.getIndustryOutput() != null && Double.valueOf(mcImportTongjiDO.getIndustryOutput()) != 0) {
                        preMuBenefitEvaluation.setIndustryOutput(mcImportTongjiDO.getIndustryOutput() == null ? null : new BigDecimal(mcImportTongjiDO.getIndustryOutput()));
                    } else {
                        preMuBenefitEvaluation.setIndustryOutput(preMuImportShuiwu.getMainIncome() == null ? null : new BigDecimal(preMuImportShuiwu.getMainIncome()));
                    }

                    preMuBenefitEvaluation.setAnnualEmployees(preMuImportRenshe.getAnnualEmployees());
                    preMuBenefitEvaluation.setVetoItemIds(vetoIds);

                    preMuBenefitEvaluation.setDelFlag("0");

//                    //计算单位用地税收得分
//                    BigDecimal sssjgx = preMuImportShuiwu.getActualTax() == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getActualTax());
//                    BigDecimal ydmj = preMuImportGuotu.getSumArea() == null ? new BigDecimal("0") : preMuImportGuotu.getSumArea();
//                    if(!(ydmj.compareTo(new BigDecimal(0)) == 0)){
//                        preMuBenefitEvaluation.setScore1(sssjgx.divide(ydmj,2,BigDecimal.ROUND_HALF_UP));
//                    }
//
//
//                    //计算单位能耗销售收入得分 - 公式：销售收入 / 总能耗
//                    //销售收入
//                    BigDecimal xssr = preMuImportShuiwu.getMainIncome()  == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getMainIncome());
//                    //总能耗
//                    BigDecimal totalUsedBigDecimal = mcImportTongjiDO.getTotalUsed() == null ? new BigDecimal("0") : new BigDecimal(mcImportTongjiDO.getTotalUsed());
//                    if(!(totalUsedBigDecimal.compareTo(new BigDecimal(0)) == 0)){
//                        preMuBenefitEvaluation.setScore2(xssr.divide(totalUsedBigDecimal,2,BigDecimal.ROUND_HALF_UP));
//                    }
//
//
//                    //计算单位污染物排放销售收入得分 - 公式：销售收入 / 主要污染排放总当量
//                    BigDecimal wrwpfdl = preMuImportHuanbao.getTotalEquivalent() == null ? new BigDecimal("0") : new BigDecimal(preMuImportHuanbao.getTotalEquivalent());
//                    if(!(wrwpfdl.compareTo(new BigDecimal(0)) == 0)){
//                        preMuBenefitEvaluation.setScore3(xssr.divide(wrwpfdl,2,BigDecimal.ROUND_HALF_UP));
//                    }
//
//
//                    //计算研发经费投入强度得分 - 公式：研发经费投入强度 = 研发经费支出 / 销售收入
//                    BigDecimal yfjfzc = preMuImportShuiwu.getRdFunds() == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getRdFunds());
//                    if(!(xssr.compareTo(new BigDecimal(0)) == 0)){
//                        preMuBenefitEvaluation.setScore4(yfjfzc.divide(xssr,2,BigDecimal.ROUND_HALF_UP));
//                    }
//
//
//                    //计算全员劳动生产力得分 - 公式：全员劳动生产率 = 工业总产值(现价) / 年平均职工人数
//                    BigDecimal gyzcz = mcImportTongjiDO.getIndustryOutput() == null ? new BigDecimal("0") : new BigDecimal(mcImportTongjiDO.getIndustryOutput());
//                    BigDecimal npjzgrs = preMuImportRenshe.getAnnualEmployees() == null ? new BigDecimal("0") : new BigDecimal(preMuImportRenshe.getAnnualEmployees());
//                    if(!(npjzgrs.compareTo(new BigDecimal(0)) == 0)) {
//                        preMuBenefitEvaluation.setScore5(gyzcz.divide(npjzgrs, 2, BigDecimal.ROUND_HALF_UP));
//                    }
//                    //计算单位水耗销售收入得分 - 沿黄地区独有 公式：单位水耗销售收入得分 = 销售收入 / 耗水量
//                    if(preMuImportShuili.getWaterBureau() != null){
//                        BigDecimal hsl = preMuImportShuili.getWaterBureau() == null ? new BigDecimal("0") :  preMuImportShuili.getWaterBureau();
//                        preMuBenefitEvaluation.setScore6(xssr.divide(hsl,2,BigDecimal.ROUND_HALF_UP));
//                    }
//
//                    //计算单位用地销售收入得分
//                    BigDecimal zydmj = preMuImportGuotu.getSumArea() == null ? new BigDecimal("0") : preMuImportGuotu.getSumArea();
//                    if(!(zydmj.compareTo(new BigDecimal(0)) == 0)) {
//                        preMuBenefitEvaluation.setScore7(xssr.divide(zydmj, 2, BigDecimal.ROUND_HALF_UP));
//                    }
                    //计算单位用地税收得分
//                    if(StringUtils.isBlank(preMuImportShuiwu.getActualTax())){
//                        throw new RuntimeException(String.format("%s 税收为空，请检查！",mcImportTongjiDO.getCompanyName()));
//                    }
//                    BigDecimal sssjgx = preMuImportShuiwu.getActualTax() == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getActualTax());
//
//
//                    BigDecimal ydmj = preMuImportGuotu.getSumArea() == null ? new BigDecimal("0") : preMuImportGuotu.getSumArea();
//                    if(!(ydmj.compareTo(new BigDecimal(0)) <= 0)){
//                        preMuBenefitEvaluation.setScore1(sssjgx.divide(ydmj,2,BigDecimal.ROUND_HALF_UP));
//                    }else{
//                        throw new RuntimeException(String.format("%s用地面积数据不全,请检查!",mcImportTongjiDO.getCompanyName()));
//                    }

                    //销售收入
                    if(StringUtils.isBlank(preMuImportShuiwu.getMainIncome())){
                        //throw new RuntimeException(String.format("%s营业收入为空,请检查!",mcImportTongjiDO.getCompanyName()));
                        correctFlag = false;
                        sb.append(String.format("%s营业收入为空,请检查!",mcImportTongjiDO.getCompanyName())).append("<br>");
                        continue;
                    }
                    BigDecimal xssr = preMuImportShuiwu.getMainIncome()  == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getMainIncome());


                    //计算单位用地销售收入得分
                    BigDecimal zydmj = preMuImportGuotu.getSumArea() == null ? new BigDecimal("0") : preMuImportGuotu.getSumArea();
                    if(!(zydmj.compareTo(new BigDecimal(0)) == 0)){
                        preMuBenefitEvaluation.setScore7(xssr.divide(zydmj,2,BigDecimal.ROUND_HALF_UP));
                    }

                    //收入利润率
                    BigDecimal totalProfitBigDecimal = preMuImportShuiwu.getProfitTotal() == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getProfitTotal());
//                    if(totalProfitBigDecimal.compareTo(BigDecimal.ZERO) <= 0){
//                        throw new RuntimeException(String.format("%s利润总额为空或为0,请检查!",mcImportTongjiDO.getCompanyName()));
//                    }
                    if(!(totalProfitBigDecimal.compareTo(new BigDecimal(0)) == 0)){
                        preMuBenefitEvaluation.setScore8(totalProfitBigDecimal.divide(xssr,4,BigDecimal.ROUND_HALF_UP));
                    }else{
                        preMuBenefitEvaluation.setScore8(BigDecimal.ZERO);
                    }


                    if("1".equals(mcImportTongjiDO.getCompanyType())){


                        //计算单位能耗销售收入得分 - 公式：销售收入 / 总能耗
                        //总能耗
                        BigDecimal totalUsedBigDecimal = mcImportTongjiDO.getTotalUsed() == null ? new BigDecimal("0") : new BigDecimal(mcImportTongjiDO.getTotalUsed());
                        if(totalUsedBigDecimal.compareTo(BigDecimal.ZERO) <= 0){
                            //throw new RuntimeException(String.format("%s总能耗为空或为0,请检查!",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s总能耗为空或为0,请检查!",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        if(!(totalUsedBigDecimal.compareTo(new BigDecimal(0)) == 0)){
                            preMuBenefitEvaluation.setScore2(xssr.divide(totalUsedBigDecimal,2,BigDecimal.ROUND_HALF_UP));
                        }

                        //计算单位污染物排放销售收入得分 - 公式：销售收入 / 主要污染排放总当量
                        if(StringUtils.isBlank(preMuImportHuanbao.getTotalEquivalent())){
                            //throw new RuntimeException(String.format("%s 主要污染物排放总当量为空，请检查！",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s 主要污染物排放总当量为空，请检查！",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        BigDecimal wrwpfdl = preMuImportHuanbao.getTotalEquivalent() == null ? new BigDecimal("0") : new BigDecimal(preMuImportHuanbao.getTotalEquivalent());
                        if(!(wrwpfdl.compareTo(new BigDecimal(0)) <= 0)){
                            preMuBenefitEvaluation.setScore3(xssr.divide(wrwpfdl,2,BigDecimal.ROUND_HALF_UP));
                        }else{
                            preMuBenefitEvaluation.setScore3(new BigDecimal("0.0000"));
                        }

                        //计算研发经费投入强度得分 - 公式：研发经费投入强度 = 研发经费支出 / 销售收入
                        if(StringUtils.isBlank(preMuImportShuiwu.getRdFunds())){
                            //throw new RuntimeException(String.format("%s 研发经费投入强度为空，请检查！",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s 研发经费投入强度为空，请检查！",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        BigDecimal yfjfzc = preMuImportShuiwu.getRdFunds() == null ? new BigDecimal("0") : new BigDecimal(preMuImportShuiwu.getRdFunds());
                        BigDecimal score4 = new BigDecimal("0.00");

                        if(xssr != null && xssr.compareTo(BigDecimal.ZERO) > 0){
                            score4 = yfjfzc.divide(xssr, 4, BigDecimal.ROUND_HALF_UP);
                        }
                        BigDecimal bigDecimal = new BigDecimal("100.0000");
                        score4 = score4.multiply(bigDecimal).setScale(4, BigDecimal.ROUND_HALF_UP);
                        preMuBenefitEvaluation.setScore4(score4);

                        //指标：全员劳动生产率 单位：万元/人   计算方式：工业总产值（现价）/年平均职工人数
                        if(StringUtils.isBlank(mcImportTongjiDO.getIndustryOutput())){
                            //throw new RuntimeException(String.format("%s 工业总产值为空，请检查！",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s 工业总产值为空，请检查！",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        Long annualEmployees = preMuImportRenshe.getAnnualEmployees();
                        if(annualEmployees == 0L){
                            //throw new RuntimeException(String.format("%s 年平均职工人数为空，请检查！",mcImportTongjiDO.getCompanyName()));
                            correctFlag = false;
                            sb.append(String.format("%s 年平均职工人数为空，请检查！",mcImportTongjiDO.getCompanyName())).append("<br>");
                            continue;
                        }
                        BigDecimal gyzcz = mcImportTongjiDO.getIndustryOutput() == null ? new BigDecimal("0") : new BigDecimal(mcImportTongjiDO.getIndustryOutput());
                        BigDecimal score5 = gyzcz.divide(BigDecimal.valueOf(annualEmployees), 2, BigDecimal.ROUND_HALF_UP);
                        preMuBenefitEvaluation.setScore5(score5);

                        //指标：单位水耗销售收入 单位：立方米/万元  计算方式：销售收入（万元）/耗水量
                        BigDecimal waterBureau = preMuImportShuili.getWaterBureau();
                        if (waterBureau != null && waterBureau.compareTo(BigDecimal.ZERO) != 0) {
                            BigDecimal score6 = xssr.divide(waterBureau, 2, BigDecimal.ROUND_HALF_UP);
                            score6 = score6.setScale(2, BigDecimal.ROUND_HALF_UP);
                            preMuBenefitEvaluation.setScore6(score6);
                        } else {
                            preMuBenefitEvaluation.setScore6(new BigDecimal("0.0000"));
                        }
                    }else{
                        BigDecimal scoreNull = new BigDecimal("0.0000");
                        preMuBenefitEvaluation.setScore2(scoreNull);
                        preMuBenefitEvaluation.setScore3(scoreNull);
                        preMuBenefitEvaluation.setScore4(scoreNull);
                        preMuBenefitEvaluation.setScore5(scoreNull);
                        preMuBenefitEvaluation.setScore6(scoreNull);
                    }

                    updateImportList.add(preMuBenefitEvaluation);

                }

                if(correctFlag){
                    if (addImportList != null && addImportList.size() >= BATCH_SIZE) {
                        preMuBenefitEvaluationMapper.saveBatch(addImportList);
                        addImportList.clear();
                        System.gc();
                    }

                    if (updateImportList != null && updateImportList.size() >= BATCH_SIZE) {
                        preMuBenefitEvaluationMapper.updateBatch(updateImportList);
                        updateImportList.clear();
                        System.gc();
                    }
                }
            }
            if(!correctFlag){
                throw new RuntimeException(sb.toString());
            }
            if (addSaveList != null && !addSaveList.isEmpty()) {
                preMuImportAddMapper.batchSave(addSaveList);
                addSaveList.clear();
                System.gc();
            }

            if (addUpdateList != null && !addUpdateList.isEmpty()) {
                preMuImportAddMapper.batchUpdate(addUpdateList);
                addUpdateList.clear();
                System.gc();
            }
            if (substractSaveList != null && !substractSaveList.isEmpty()) {
                preMuImportSubstractMapper.batchSave(substractSaveList);
                substractSaveList.clear();
                System.gc();
            }
            if (substractUpdateList != null && !substractUpdateList.isEmpty()) {
                preMuImportSubstractMapper.batchUpdate(substractUpdateList);
                substractUpdateList.clear();
                System.gc();
            }

            if (addImportList != null && !addImportList.isEmpty()) {
                preMuBenefitEvaluationMapper.saveBatch(addImportList);
                addImportList.clear();
                System.gc();
            }

            if (updateImportList != null && !updateImportList.isEmpty()) {
                preMuBenefitEvaluationMapper.updateBatch(updateImportList);
                updateImportList.clear();
                System.gc();
            }
        }else {
            if (params.get("sign") != null && !"".equals(params.get("sign"))) {
                return AjaxResult.error("本年度 " + params.get("sign") + "尚未导入，请先导入该部门数据！ ");
            }
        }
        params.put("year", year);

        PreMuImportBenefitEvaluationNo queryNoCpData = new PreMuImportBenefitEvaluationNo();
        queryNoCpData.setYear(Long.valueOf(year));
        queryNoCpData.setCounty(params.get("county"));
        queryNoCpData.setCompanyType(params.get("companyType"));
        List<PreMuImportBenefitEvaluationNo> preMuImportBenefitEvaluationNos = preMuImportBenefitEvaluationNoMapper.selectPreMuImportBenefitEvaluationNoList(queryNoCpData);

        if(preMuImportBenefitEvaluationNos != null && !preMuImportBenefitEvaluationNos.isEmpty()){

            List<PreMuImportBenefitEvaluationNo> updateImportEvaluationNoList = new ArrayList<>();
            List<PreMuImportBenefitEvaluationNo> addImportEvaluationNoList = new ArrayList<>();

            for(PreMuImportBenefitEvaluationNo temp : preMuImportBenefitEvaluationNos){
                PreMuCompanyInfo companyInfo = comMap.get(temp.getUscc());
                if(companyInfo != null){
                    temp.setIndustryType(companyInfo.getIndustryType());
                    temp.setIndustryMediumType(companyInfo.getIndustryMediumType());
                }
                if (noCpExistMap != null && noCpExistMap.containsKey(temp.getUscc())) {

                    PreMuCompanyInfo preMuCompanyInfo = comMap.get(temp.getUscc());

                    PreMuBenefitEvaluationNo tempObj = (PreMuBenefitEvaluationNo)noCpExistMap.get(temp.getUscc());
                    temp.setId(tempObj.getId());
                    temp.setDelFlag("0");

                    //设置行政区
                    String county = temp.getCounty();
                    if(StringUtils.isNotBlank(county) && county.length() == 6){
                        temp.setProvince(StringUtils.left(county,2) + "0000");
                        temp.setProvinceName(redisTemplate.opsForValue().get("area:" + temp.getProvince()));
                        temp.setCity(StringUtils.left(county,4) + "00");
                        temp.setCityName(redisTemplate.opsForValue().get("area:" + temp.getCity()));
                        temp.setCountyName(redisTemplate.opsForValue().get("area:" + temp.getCounty()));
                    }

                    //设置业企规模名称
                    String companyScaleName = DictUtils.getDictLabel("bus_enter_scale", temp.getCompanyScale());
                    temp.setCompanyScaleName(companyScaleName);
                    //设置企业类型
                    String companyType = DictUtils.getDictLabel("enter_type", temp.getCompanyType());
                    temp.setCompanyTypeName(companyType);

                    //设置行业类型
                    String industryTypeName = DictUtils.getDictLabel("industry_names", temp.getIndustryType());
                    temp.setIndustryTypeName(industryTypeName);

                    if(preMuCompanyInfo != null && StringUtils.isNotBlank(preMuCompanyInfo.getVillage())){
                        temp.setVillage(preMuCompanyInfo.getVillage());
                        temp.setVillageName(redisTemplate.opsForValue().get("area:" + preMuCompanyInfo.getVillage()));
                    }

                    updateImportEvaluationNoList.add(temp);
                }else if(comMap.containsKey(temp.getUscc())){

                    PreMuCompanyInfo preMuCompanyInfo = comMap.get(temp.getUscc());

                    temp.setId(UUID.randomUUID().toString().replace("-", ""));
                    temp.setDelFlag("0");
                    //设置行政区
                    String county = temp.getCounty();
                    if(StringUtils.isNotBlank(county) && county.length() == 6){
                        temp.setProvince(StringUtils.left(county,2) + "0000");
                        temp.setProvinceName(redisTemplate.opsForValue().get("area:" + temp.getProvince()));
                        temp.setCity(StringUtils.left(county,4) + "00");
                        temp.setCityName(redisTemplate.opsForValue().get("area:" + temp.getCity()));
                        temp.setCountyName(redisTemplate.opsForValue().get("area:" + temp.getCounty()));
                    }

                    //设置业企规模名称
                    String companyScaleName = DictUtils.getDictLabel("bus_enter_scale", temp.getCompanyScale());
                    temp.setCompanyScaleName(companyScaleName);
                    //设置企业类型
                    String companyType = DictUtils.getDictLabel("enter_type", temp.getCompanyType());
                    temp.setCompanyTypeName(companyType);
                    //设置行业类型
                    String industryTypeName = DictUtils.getDictLabel("industry_names", temp.getIndustryType());
                    temp.setIndustryTypeName(industryTypeName);

                    if(preMuCompanyInfo != null && StringUtils.isNotBlank(preMuCompanyInfo.getVillage())){
                        temp.setVillage(preMuCompanyInfo.getVillage());
                        temp.setVillageName(redisTemplate.opsForValue().get("area:" + preMuCompanyInfo.getVillage()));
                    }

                    addImportEvaluationNoList.add(temp);
                }

                if(updateImportEvaluationNoList.size() > 100){
                    preMuBenefitEvaluationNoMapper.batchUpdate(updateImportEvaluationNoList);
                    updateImportEvaluationNoList.clear();
                }

                if(addImportEvaluationNoList.size() > 100){
                    preMuBenefitEvaluationNoMapper.batchSave(addImportEvaluationNoList);
                    addImportEvaluationNoList.clear();
                }
            }
            if(!updateImportEvaluationNoList.isEmpty()){
                preMuBenefitEvaluationNoMapper.batchUpdate(updateImportEvaluationNoList);
                updateImportEvaluationNoList.clear();
            }

            if(!addImportEvaluationNoList.isEmpty()){
                preMuBenefitEvaluationNoMapper.batchSave(addImportEvaluationNoList);
                addImportEvaluationNoList.clear();
            }
        }

        PreMuImportBenefitEvaluation pjjgQueryParam = new PreMuImportBenefitEvaluation();
        pjjgQueryParam.setYear(year);
        pjjgQueryParam.setCompanyType(params.get("companyType"));
        pjjgQueryParam.setCounty(params.get("county"));

        List<PreMuImportBenefitEvaluation> mcImportBenefitEvaluationDOs = preMuImportBenefitEvaluationService.selectPreMuImportBenefitEvaluationList(pjjgQueryParam);

        if (mcImportBenefitEvaluationDOs != null && mcImportBenefitEvaluationDOs.size() > 0) {
            for (PreMuImportBenefitEvaluation mcImportBenefitEvaluationDO : mcImportBenefitEvaluationDOs) {
                PreMuCompanyInfo companyInfo = comMap.get(mcImportBenefitEvaluationDO.getUscc());
                if(companyInfo != null){
                    mcImportBenefitEvaluationDO.setIndustryType(companyInfo.getIndustryType());
                    mcImportBenefitEvaluationDO.setIndustryMediumType(companyInfo.getIndustryMediumType());
                }
                if (mcImportExistMap != null && mcImportExistMap.containsKey(mcImportBenefitEvaluationDO.getUscc())) {
                    mcImportBenefitEvaluationDO.setId((String) mcImportExistMap.get(mcImportBenefitEvaluationDO.getUscc()));
                    mcImportBenefitEvaluationDO.setDelFlag("0");
                    updateImportEvaluationList.add(mcImportBenefitEvaluationDO);
                }else if(comMap.containsKey(mcImportBenefitEvaluationDO.getUscc())){
                    mcImportBenefitEvaluationDO.setId(UUID.randomUUID().toString().replace("-", ""));
                    mcImportBenefitEvaluationDO.setDelFlag("0");
                    addImportEvaluationList.add(mcImportBenefitEvaluationDO);
                }

                if (addImportEvaluationList != null && addImportEvaluationList.size() >= BATCH_SIZE) {
//                    mcBenefitEvaluationDao.batchSaveByImportInfo(addImportEvaluationList);
                    preMuBenefitEvaluationMapper.batchSaveByImportInfo(addImportEvaluationList);
                    addImportEvaluationList.clear();
                    System.gc();
                }
                if (updateImportEvaluationList != null && updateImportEvaluationList.size() >= BATCH_SIZE) {

                    preMuBenefitEvaluationMapper.batchUpdateByImportInfo(updateImportEvaluationList);
                    updateImportEvaluationList.clear();
                    System.gc();
                }
            }

            if (addImportEvaluationList != null && !addImportEvaluationList.isEmpty()) {
                preMuBenefitEvaluationMapper.batchSaveByImportInfo(addImportEvaluationList);
                addImportEvaluationList.clear();
                System.gc();
            }
            if (updateImportEvaluationList != null && !updateImportEvaluationList.isEmpty()) {

                preMuBenefitEvaluationMapper.batchUpdateByImportInfo(updateImportEvaluationList);
                updateImportEvaluationList.clear();
                System.gc();
            }

        }
        return AjaxResult.success();
    }

    @Override
    public PreMuImportLog getLogByDeptCodeAndYearAndCounty(String deptCode, String year, String county) {
        return preMuImportLogMapper.selectLogByDeptCodeAndYearAndCounty(deptCode, year, county);
    }

    @Override
    public void insertOrUpdateLog(PreMuImportLog log) {
        String deptCode = log.getDeptCode();
        String year = log.getYear();
        String county = log.getCounty();
        PreMuImportLog importLog = getLogByDeptCodeAndYearAndCounty(deptCode, year, county);
        if(importLog == null) {
            log.setId(IdUtil.simpleUUID());
            preMuImportLogMapper.insertPreMuImportLog(log);
        } else {
            log.setId(importLog.getId());
            preMuImportLogMapper.addPreMuImportLogNum(log);
        }
    }
}
